package org.net.loader
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	/**
	 * SWF Loader
	 * 
	 */
	public class SwfLoader extends ResLoader
	{
		private var _loader : Loader;
		private var _domain : ApplicationDomain;
		
		
		public function SwfLoader(data : ResourceData)
		{
			super(data);
		}
		
		
		public function get loader() : Loader {
			return _loader;
		}
		
		
		override protected function onComplete() : void {
			_loader = new Loader();
			var context : LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			_loader.loadBytes(_byteArray, context);
		}
		
		
		private function completeHandler(event : Event) : void {
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
			_domain = LoaderInfo(event.currentTarget).applicationDomain;
			_isLoading = false;
			_isLoaded = true;
			trace("load complete:" + _resData.url);
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		
		/**
		 * getClass 从SWF中获得类
		 * 
		 * @param className 类名称
		 * @return Class 类
		 */
		public function getClass(className : String) : Class {
			if (!_domain.hasDefinition(className)) {
				return null;
			}
			var assetClass : Class = _domain.getDefinition(className) as Class;
			return assetClass;
		}
		
		
		
		/**
		 * 获得MovieClip
		 * 
		 * @param className 元件名
		 * @return MovieClip
		 */
		public function getMovieClip(className : String) : MovieClip {
			var assetClass : Class = getClass(className);
			if (assetClass == null) {
				return null;
			}
			var mc : MovieClip = new assetClass() as MovieClip;
			if (mc == null) {
				trace("can't find the MovieClip:"+ className);
			}
			return mc;
		}
	}
}